package com.ghostwan.androears.adapter;

import java.util.ArrayList;

import com.ghostwan.androears.AndroEars;
import com.ghostwan.androears.R;
import com.ghostwan.androears.customObject.Artist;
import com.ghostwan.androears.customObject.Song;

import android.content.Context;
import android.os.RemoteException;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class ArtistAdapter extends BaseAdapter
{

	private final LayoutInflater mInflater;
	private final int mLayoutRes;
	public ArrayList<Artist> mArtistList;
	public AndroEars mAndroEars;
	protected String TAG = "ArtistAdapter";

	public ArtistAdapter(AndroEars androEars, int textViewResourceId)
	{
		super();
		mAndroEars = androEars;
		mInflater = (LayoutInflater) mAndroEars.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mLayoutRes = textViewResourceId;
		mArtistList = new ArrayList<Artist>();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{

		View view;
		if (convertView == null)
		{
			view = mInflater.inflate(mLayoutRes, parent, false);
		}
		else
		{
			view = convertView;
		}
		Artist artist = this.getItem(position);
		LinearLayout addLayout = (LinearLayout) view.findViewById(R.id.addLayout);
		addLayout.setClickable(true);
		addLayout.setTag(new Integer(position));
		// addLayout.setFocusable(true);
		addLayout.setBackgroundResource(android.R.drawable.menuitem_background);
		addLayout.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				try
				{
					Artist artist = mArtistList.get((Integer) v.getTag());
					ArrayList<Song> songList = mAndroEars.getSongList(artist, null);
					for (Song song : songList)
					{
						mAndroEars.mPlayerService.addSongPlaylist(song);
					}
					mAndroEars.mMusicPlayerButton.setVisibility(View.VISIBLE);
					String s = new String();
					s = mAndroEars.getString(R.string.adding) + " " + artist.mName
							+ mAndroEars.getString(R.string.adding_multiple_song_cp);
					Toast.makeText(mAndroEars, s, Toast.LENGTH_LONG).show();
				}
				catch (RemoteException e)
				{
					Log.e(TAG, e.getMessage());
				}

			}
		});
		((TextView) view.findViewById(R.id.artistName)).setText(artist.mName);
		if (artist.mAlbumCount == 1)
			((TextView) view.findViewById(R.id.albumCount)).setText(artist.mAlbumCount + " album");
		else
			((TextView) view.findViewById(R.id.albumCount)).setText(artist.mAlbumCount + " albums");

		// TODO Auto-generated method stub
		return view;
	}

	public void setArtistList(ArrayList<Artist> list)
	{
		mArtistList = list;
		this.notifyDataSetChanged();
	}

	@Override
	public int getCount()
	{
		return mArtistList.size();
	}

	@Override
	public Artist getItem(int arg0)
	{
		return mArtistList.get(arg0);
	}

	@Override
	public long getItemId(int arg0)
	{
		return arg0;
	}

}
